package cn.mofuxiong.action;




import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.LinkedHashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import cn.common.tool.Head;
import cn.common.tool.JsonObject;
import cn.mofuxiong.Impl.UserDao;
import cn.mofuxiong.Model.login_register;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class Login_Register_Action extends ActionSupport{
	private static final long serialVersionUID = -8896844723887171899L;
	private login_register login;
	boolean flag=false;
	HttpServletRequest req = ServletActionContext.getRequest();
	HttpServletResponse response=ServletActionContext.getResponse();

   public String login(){ //用户登录
	  
	   UserDao ud = new UserDao();
	   int iaction = Integer.parseInt(req.getParameter("select1"));//获取身份登录
	   System.out.println(iaction);
	   ResultSet rs = ud.login(login.getA_userId(),login.getA_passwd(),iaction);
	   System.out.println("账号："+login.getA_userId()+"密码："+login.getA_passwd());
	   
	   switch (iaction) {
	 
	   case 1:	
		   try{
	         	      
			   if(rs.next()&&rs!=null)
			   {	
				   req.getSession().setAttribute("UserId_id",rs.getString("a_id"));
				   req.getSession().setAttribute("UserId",login.getA_userId());	
				   return "manager";
			   }			 
			   else{
				   return "shabi";
			   }
				  
          }	    
	   catch(Exception e)
		   {
			   System.out.println(e.getMessage()+"200");
		    }
		   
	   case 0:
		try
		   {
			
			  if(rs.next()&&rs!=null)
			  {		
				  req.getSession().setAttribute("UserId_id",rs.getString("a_id"));
				   req.getSession().setAttribute("UserId",login.getA_userId());	
				   return "kefu";
			   }			 
			   else{
				   return "shabi";
			   }
		   }
		catch(Exception e)
			{
				System.out.println(e.getMessage()+"500");
			}break;
		   
		   
	   }
	return null;
   }




@SuppressWarnings("unused")
public String register() throws Exception{     //用户注册
	//String  status='0';
	UserDao logindao = new UserDao();
	int pd = logindao.register(login.getA_name(),login.getA_passwd());
	if(pd==1)
		return SUCCESS;
	else
	return ERROR;
	
}

 public String ValidateCode(){    
	 try{
		 String code=req.getParameter("p");
		 String verifycode=(String) req.getSession().getAttribute("verifycode");
		 System.out.println(code+"===="+verifycode);//获取验证码
	if(verifycode.contains(code)&&!("".equals(code)))
	{
		flag=true;		
	}
	else
	{
		flag=false;
	}
	 
	 response.setCharacterEncoding("utf-8");
     response.setContentType("text/html");
     PrintWriter out=response.getWriter();
       if(flag){
      
            out.write(JSONArray.fromObject(new JsonObject(new Head(200,"验证成功",false),null)).toString());
         }else{
         
            out.write(JSONArray.fromObject(new JsonObject(new Head(500,"验证失败",false),null)).toString());
          }
         out.flush();//刷新流
         out.close();//关闭流
         }
	 catch(Exception e)
	 {
             System.out.println(e+"000000");
     }	
	 
	 return null;
 }
      

		@SuppressWarnings({ "rawtypes", "unused" })
		public String checkId(){//异步校验用户名
	try{    
		    String userId=req.getParameter("p");
			UserDao userdao= new UserDao();			
			LinkedHashMap map=userdao.select(userId);
			if(map!=null)//已存在此用户名
			{
				flag=true;
			}
			else	//此用户名可用
			{
				flag=false;
			}
			 response.setCharacterEncoding("utf-8");
		     response.setContentType("text/html");
		     PrintWriter out=response.getWriter();
		       if(flag){
		      
		            out.write(JSONArray.fromObject(new JsonObject(new Head(200,"用户名已存在",false),null)).toString());
		         }else{
		         
		            out.write(JSONArray.fromObject(new JsonObject(new Head(500,"用户名可用",false),null)).toString());
		          }
		         out.flush();//刷新流
		         out.close();//关闭流
		         }
			 catch(Exception e)
			 {
		             System.out.println(e+"异步有错");
		     }	
			 
			return null;
		}
		
 
       public String tuichu(){
    	   req.getSession().invalidate();
    	   return "ok";
    			 
       }
 
 
        public login_register getLogin()
        {
			return login;
		}		
		public void setLogin(login_register login) 
		{
			this.login = login;
		}
		



}


